package redis

import (
	"context"
	"fmt"
	"shop_srv/shop"
	"strconv"
)

func CartAdd(cart map[string]interface{}) error {
	key := fmt.Sprintf("cart:user_id_%d:goods_id_%d", cart["userId"], cart["goodsId"])
	return Client.HMSet(context.Background(), key, cart).Err()
}
func IsExt(userId int64, GoodsId int64) bool {
	key := fmt.Sprintf("cart:user_id_%d:goods_id_%d", userId, GoodsId)
	return Client.Exists(context.Background(), key).Val() == 1
}
func Add(userId int64, GoodsId int64, num int64) error {
	key := fmt.Sprintf("cart:user_id_%d:goods_id_%d", userId, GoodsId)
	return Client.HIncrBy(context.Background(), key, "num", num).Err()
}

func DelCart(userId int64, GoodsId int64) error {
	key := fmt.Sprintf("cart:user_id_%d:goods_id_%d", userId, GoodsId)
	return Client.Del(context.Background(), key).Err()
}
func ClearCart(userId int64) {
	key := fmt.Sprintf("cart:user_id_%d:*", userId)
	keys := Client.Keys(context.Background(), key).Val()
	for _, s := range keys {
		Client.Del(context.Background(), s)
	}
}
func Check(userId int64, check int64) {
	key := fmt.Sprintf("cart:user_id_%d:*", userId)
	keys := Client.Keys(context.Background(), key).Val()
	for _, s := range keys {
		Client.HMSet(context.Background(), s, "check", check)
	}
}
func Sum(userId int64) (count int, price float64, check int64) {
	key := fmt.Sprintf("cart:user_id_%d:*", userId)
	keys := Client.Keys(context.Background(), key).Val()
	for _, s := range keys {
		GoodsMap := Client.HGetAll(context.Background(), s).Val()
		num, _ := strconv.Atoi(GoodsMap["num"])
		checks, _ := strconv.Atoi(GoodsMap["check"])
		prices, _ := strconv.ParseFloat(GoodsMap["price"], 64)
		if checks == 1 {
			count += num
			price += prices * float64(num)
		}
	}
	return
}

func CartList(userId int64) []*shop.GoodsList {
	key := fmt.Sprintf("cart:user_id_%d:*", userId)
	keys := Client.Keys(context.Background(), key).Val()
	var cart []*shop.GoodsList
	for _, s := range keys {
		GoodsMap := Client.HGetAll(context.Background(), s).Val()
		userIds, _ := strconv.Atoi(GoodsMap["userId"])
		goosIds, _ := strconv.Atoi(GoodsMap["goodsId"])
		nums, _ := strconv.Atoi(GoodsMap["num"])
		prce, _ := strconv.ParseFloat(GoodsMap["price"], 64)
		title, _ := GoodsMap["title"]
		list := shop.GoodsList{
			UserId:  int64(userIds),
			GoodsId: int64(goosIds),
			Title:   title,
			Price:   float32(prce),
			Num:     int64(nums),
		}
		cart = append(cart, &list)
	}
	return cart
}
func List(userId int64, goodsId int64) []*shop.GoodsLists {
	key := fmt.Sprintf("cart:user_id_%d:goods_id_%d", userId, goodsId)
	keys := Client.Keys(context.Background(), key).Val()
	var cart []*shop.GoodsLists
	for _, s := range keys {
		GoodsMap := Client.HGetAll(context.Background(), s).Val()
		userIds, _ := strconv.Atoi(GoodsMap["userId"])
		goosIds, _ := strconv.Atoi(GoodsMap["goodsId"])
		nums, _ := strconv.Atoi(GoodsMap["num"])
		prce, _ := strconv.ParseFloat(GoodsMap["price"], 64)
		title, _ := GoodsMap["title"]
		list := shop.GoodsLists{
			UserId:  int64(userIds),
			GoodsId: int64(goosIds),
			Title:   title,
			Price:   float32(prce),
			Num:     int64(nums),
		}
		cart = append(cart, &list)
	}
	return cart
}
